<?php

namespace App\Admin\Widgets;

use Closure;
use Dcat\Admin\Support\Helper;
use Dcat\Admin\Widgets\Modal as BaseModal;

class Modal extends BaseModal
{
    public $className;
    public function addButtonClass($className = '')
    {
        $this->className = $className;
        return $this;
    }

    protected function renderFooter()
    {
        $footer = Helper::render($this->footer);

        if (! $footer) {
            return;
        }

        return <<<HTML
<div class="modal-footer">{$footer}</div>
HTML;
    }

    protected function renderButton()
    {
        if (! $this->button) {
            return;
        }

        $button = Helper::render($this->button);

        // 如果没有HTML标签则添加一个 a 标签
        if (! preg_match('/(\<\/[\d\w]+\s*\>+)/i', $button)) {
            $button = "<a href=\"javascript:void(0)\">{$button}</a>";
        }

        return <<<HTML
<span style="cursor: pointer" class="{$this->className}" data-toggle="modal" data-target="#{$this->id()}">{$button}</span>
HTML;
    }
}
